// 增删改查

import { articleModel } from "../Model/articleModel.js"


export const addArtDao = async (article) => {
   return await articleModel.create(article);
}

export const delArtDao = async (id) => {
    return await articleModel.findOneAndDelete(id);
}

export const updateArtDao = async (id, obj) => {
    return await articleModel.findByIdAndUpdate(id, obj);
}
/**
 * 
 * @param {Number} page 
 * @param {Number} size 
 * @param {
 *      typename 类型名称
 *      title 文章标题 模糊查找 
 * } queryobj 
 */
export const getArtByPageDao = async (page, size, queryobj ={}) => {
  if(queryobj.title) {
    queryobj.title = new RegExp(queryobj.title, 'i');
  }
  const list  = await articleModel.find(queryobj).skip((page-1)*size).limit(size);
  const total = await articleModel.find(queryobj).countDocuments();
  return {
    list,
    total
  }
}

export const getArtByIdDao = async (id) => {
  await articleModel.findByIdAndUpdate(id,{$inc: {pv: 1}})
    return await articleModel.findById(id)
}